var t = getApp();

module.exports = Behavior({
    data: {
        address_a: "",
        point: {
            lat: "",
            lng: ""
        },
        city: ""
    },
    methods: {
        getyz_basic_info: function() {
            return wx.getStorageSync("yz_basic_info") ? wx.getStorageSync("yz_basic_info").thumb_parameter : "";
        },
        _getLocation: function(o, a) {
            var e = this;
            t.globalData.isLocationIng ? setTimeout(function() {
                e._getLocation(o);
            }, 100) : (t.globalData.isLocationIng = !0, wx.getSetting({
                success: function(t) {
                    t.authSetting["scope.userLocation"] ? e.getlocaldata(o, a) : wx.authorize({
                        scope: "scope.userLocation",
                        success: function() {},
                        fail: function(t) {},
                        complete: function() {
                            e.getlocaldata(o, a);
                        }
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            }));
        },
        getlocaldata: function(o, a) {
            var e = this;
            if (wx.getStorageSync("address-point")) {
                var n = wx.getStorageSync("address-point").point;
                this._setaddressData({
                    lat: n.lat,
                    lng: n.lng,
                    from: "storage"
                }, o);
            } else wx.getLocation({
                type: "gcj02",
                success: function(t) {
                    var a = t.latitude, n = t.longitude;
                    e._setaddressData({
                        lat: a,
                        lng: n
                    }, o);
                },
                fail: function(n) {
                    "getLocation:fail auth deny" === n.errMsg ? (console.log("当初用户拒绝，再次发起授权"), wx.openSetting({
                        success: function(a) {
                            a.authSetting["scope.userLocation"] ? (wx.showToast({
                                icon: "none",
                                title: "获取权限成功",
                                duration: 1500
                            }), e.getlocaldata(o)) : (t.globalData.isLocationIng = !1, wx.showToast({
                                icon: "none",
                                title: "获取权限失败,定位需要用户授权",
                                duration: 1500
                            }));
                        },
                        fail: function(t) {
                            try {
                                e.is_errCall(a);
                            } catch (t) {
                                console.log(t);
                            }
                        }
                    })) : t.globalData.isLocationIng = !1;
                }
            });
        },
        is_errCall: function(o) {
            t.globalData.isLocationIng = !1, o(!1);
        },
        _setaddressData: function(o, a) {
            var e = this;
            t.getReverseGeocoder(o.lat, o.lng).then(function(n) {
                var s = n;
                e.setData({
                    "point.lat": o.lat || "",
                    "point.lng": o.lng || "",
                    address_a: s.address,
                    title: s.formatted_addresses && s.formatted_addresses.recommend ? s.formatted_addresses.recommend : "",
                    city: t._isTextEmpty(s.address_component.city) ? t._isTextEmpty(s.address_component.province) ? "" : s.address_component.province : s.address_component.city
                }), "storage" === o.from ? (t.globalData.isLocationIng = !1, a && a(s, o)) : wx.setStorage({
                    key: "address-point",
                    data: {
                        point: e.data.point,
                        city: e.data.city,
                        adcode: s.ad_info.adcode,
                        address_component: s.address_component
                    },
                    success: function(e) {
                        t.globalData.isLocationIng = !1, a && a(s, o);
                    }
                });
            }).catch(function(o) {
                t.globalData.isLocationIng = !1, console.log(o);
            });
        }
    }
});